package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Resource
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public void update(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public void delete(Long id) {
        nursingLevelMapper.deleteById(id);

    }

    @Override
    public void changeStatus(NursingLevelDto dto) {
        nursingLevelMapper.status(dto.getId(), dto.getStatus());
    }
    @Override
    public ArrayList<NursingLevelVo> listAll() {
        ArrayList<NursingLevel> nl = nursingLevelMapper.listAll();
        ArrayList<NursingLevelVo> nlvList = (ArrayList<NursingLevelVo>) nl.stream().map(nlItem -> BeanUtil.toBean(nlItem, NursingLevelVo.class)).collect(Collectors.toList());
        return nlvList;
    }

    //新增
    @Transactional
    @Override
    public void insert( NursingLevel nursingLevel) {
        nursingLevelMapper.insert(nursingLevel);

    }

    @Override
    public PageResponse<NursingLevelVo> listByPage(String name, Integer status, Integer pageNum, Integer pageSize) {


        PageHelper.startPage(pageNum, pageSize);


        Page<NursingLevel> np = nursingLevelMapper.listByPage(name, status);


        return PageResponse.of(np, NursingLevelVo.class);
    }

    @Override
    public NursingLevelVo getById(Long id) {
        NursingLevel nl= nursingLevelMapper.getById(id);
        NursingLevelVo nlv = BeanUtil.toBean(nl, NursingLevelVo.class);
        return nlv;
    }
}
